// 引入用来发送请求接口的方法
import { request } from "../../request/index.js"

Page({
  data: {
    //轮播图数组
    swiperList: [],
    cateList: [],//导航数组数据
    floorList:[],//楼层数据
  },
  //页面开始加载就会触发  等同created
  onLoad: function (options) {
    //获取首页的轮播图数据  使用es6 promise技术封装请求路径的函数
    // wx.request({
    //   url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata',
    //   success: (result) => {
    //     console.log(result)
    //     this.setData({
    //       swiperList: result.data.message
    //     })
    //   },
    //  complete: ()=>{} 无论成功还是失败都会执行
    // });

    // request({
    //   url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata'
    // }).then(result => {
    //   this.setData({
    //     swiperList: result.data.message
    //   })
    // })
    this.getSwiperList()
    this.getCateList()
    this.getFloorList()

  },

  async getSwiperList() {
    const { data: res } = await request({
      url: '/home/swiperdata'
    })
    this.setData({
      swiperList: res.message
    })
  },

  getCateList() {
    request({
      url: '/home/catitems'
    }).then(result => {
      this.setData({
        cateList: result.data.message
      })
    })
  },

  getFloorList() {
    request({
      url: '/home/floordata'
    }).then(result => {
      for (let k = 0; k < result.data.message.length; k++) {
        result.data.message[k].product_list.forEach((v, i) => {
          result.data.message[k].product_list[i].navigator_url = v.navigator_url.replace('?','/index?');
        });
    }
      this.setData({
        floorList: result.data.message
      })
    })
  }
});